const http = require('http');

http.createServer((req,res)=>{

    // 1. 获取到地址栏网址
    let url = req.url;

    if(url === '/favicon.ico'){
        res.end('1');
        return;
    }
    // console.log(url);

    // console.log(typeof url); //string

    // 获取地址栏中的查询字符串

    let aUrl = new URL(url,"http://localhost:8081");
    // console.log(aUrl);

    // URL {
    //     href: 'http://localhost:8081/list?name=abc&age=18',
    //     origin: 'http://localhost:8081',
    //     protocol: 'http:',
    //     username: '',
    //     password: '',
    //     host: 'localhost:8081',
    //     hostname: 'localhost',
    //     port: '8081',
    //     pathname: '/list/a/b',
    //     search: '?name=abc&age=18',
    //     searchParams: URLSearchParams { 'name' => 'abc', 'age' => '18' },
    //     hash: ''
    //   }
      


    // console.log(aUrl.searchParams); //{ 'name' => 'laowang', 'age' => '18', 'sex' => '男' }

    let params = aUrl.searchParams; //本身是一个map数据结构

    // console.log(params.name); //undefined

    console.log(params.get('name')); //laowang

    // URL {
    //     href: 'http://localhost:8081/search?name=laowang&age=18&sex=%E7%94%B7',
    //     origin: 'http://localhost:8081',
    //     protocol: 'http:',
    //     username: '',
    //     password: '',
    //     host: 'localhost:8081',
    //     hostname: 'localhost',
    //     port: '8081',
    //     pathname: '/search',
    //     search: '?name=laowang&age=18&sex=%E7%94%B7',
    //     searchParams: URLSearchParams { 'name' => 'laowang', 'age' => '18', 'sex' => '男' },
    //     hash: ''
    //   }
    
    res.end('123');

}).listen(8081,()=>{
    console.log("8081启动成功");
});